# coding=utf-8
# 编译日期：2023-02-06 10:34:14
# 版权所有：www.i-search.com.cn
import ubpa.init_input as iinput
from ubpa.base_util import StdOutHook, ExceptionHandler
import ubpa.ics as ics
import ubpa.icsv as icsv
import ubpa.iexcel as iexcel
import ubpa.itools.rpa_environment as rpa_environment
import ubpa.irobot as irobot
import ubpa.ichrome_firefox as ichrome_firefox
import ubpa.iwin as iwin
import ubpa.ibrowse as ibrowse
import ubpa.ibox as ibox
import time
import pdb
from ubpa.ilog import ILog
import getopt
from sys import argv
import sys
import os
from ubpa.base_img import *

class GetBilibiliData:
     
    def __init__(self,**kwargs):
        self.__logger = ILog(__file__)
        self.path = set_img_res_path(__file__)
        self.robot_no = ''
        self.proc_no = ''
        self.job_no = ''
        self.input_arg = ''
        if('robot_no' in kwargs.keys()):
            self.robot_no = kwargs['robot_no']
        if('proc_no' in kwargs.keys()):
            self.proc_no = kwargs['proc_no']
        if('job_no' in kwargs.keys()):
            self.job_no = kwargs['job_no']
        ILog.JOB_NO, ILog.OLD_STDOUT = self.job_no, sys.stdout
        sys.stdout = StdOutHook(self.job_no, sys.stdout)
        ExceptionHandler.JOB_NO, ExceptionHandler.OLD_STDERR = self.job_no, sys.stderr
        sys.excepthook = ExceptionHandler.handle_exception
        if('input_arg' in kwargs.keys()):
            self.input_arg = kwargs['input_arg']
            if(len(self.input_arg) <= 0):
                self.input_arg = iinput.load_init(__file__)
            if self.input_arg is None:
                sys.exit(0)
        self.gv_1=None
      
    def Main(self,pv_1=None):
        #消息框
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022121610323921553,Title:消息框,Note:')
        tvar_2022121610323921754=ibox.msgs_box('此流程需安装chrome浏览器，若未安装会导致流程失败',timeout=5)
        print('[Main] [消息框] [SNTag:2022121610323921553]  返回值：[' + str(type(tvar_2022121610323921754)) + ']' + str(tvar_2022121610323921754))
        #打开bilibili网页
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207135722100385,Title:打开bilibili网页,BzTag:93719b89537f498a89643f2b9fa9ad89,Note:')
        irobot.send_robot_workflow(sz_tag='93719b89537f498a89643f2b9fa9ad89',sz_title='打开bilibili网页')
        #打开浏览器
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120610103766241,Title:打开浏览器,Note:此流程需安装chrome浏览器')
        time.sleep(1)
        tvar_2022120610103766242=ibrowse.open_browser(browser_type='chrome',url='www.bilibili.com')
        print('[Main] [打开浏览器] [SNTag:2022120610103766241]  返回值：[' + str(type(tvar_2022120610103766242)) + ']' + str(tvar_2022120610103766242))
        #最大化窗口
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120610115344244,Title:最大化窗口,Note:')
        time.sleep(1)
        tvar_2022120610115344245=iwin.do_win_maximize(waitfor=1.000,win_title=r'哔哩哔哩 (゜-゜)つロ 干杯~-bilibili - Google Chrome')
        print('[Main] [最大化窗口] [SNTag:2022120610115344244]  返回值：[' + str(type(tvar_2022120610115344245)) + ']' + str(tvar_2022120610115344245))
        #查询“艺赛旗RPA”账户
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207135821372390,Title:查询“艺赛旗RPA”账户,BzTag:29c53828c21e413f8caa1d1f77c6a391,Note:')
        irobot.send_robot_workflow(sz_tag='29c53828c21e413f8caa1d1f77c6a391',sz_title='查询“艺赛旗RPA”账户')
        #设置文本
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221206110214409231,Title:设置文本,Note:')
        time.sleep(1)
        ichrome_firefox.set_element_val_chrome(waitfor=10.000,text=r'艺赛旗RPA',title=r'哔哩哔哩 (゜-゜)つロ 干杯~-bilibili',url=r'https://www.bilibili.com/',attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"css-selector":"#nav-searchform > div:nth-child(1) > input:nth-child(1)","tag":"INPUT","xpath":"//*[@id=\"nav-searchform\"]/div[1]/input[1]"},"index":0,"tagName":"INPUT"}}]})
        #鼠标点击
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221206110249815236,Title:鼠标点击,Note:')
        time.sleep(1)
        ichrome_firefox.do_click_pos_chrome(waitfor=10.000,run_mode='unctrl',button='left',curson='center',continue_on_error='break',title=r'哔哩哔哩 (゜-゜)つロ 干杯~-bilibili',url=r'https://www.bilibili.com/',attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"css-selector":"#nav-searchform > div:nth-child(2)","parentid":"nav-searchform","tag":"DIV","xpath":"//*[@id=\"nav-searchform\"]/div[2]"},"index":0,"tagName":"DIV"}}]})
        #鼠标点击
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221206143607406283,Title:鼠标点击,Note:')
        time.sleep(1)
        ichrome_firefox.do_click_pos_chrome(waitfor=10.000,run_mode='unctrl',button='left',curson='center',continue_on_error='break',title=r'艺赛旗RPA-哔哩哔哩_Bilibili',url=r'https://search.bilibili.com/*',attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"aaname":"用户","css-selector":"#i_cecream > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > nav:nth-child(1) > ul:nth-child(1) > li:nth-child(8) > span:nth-child(1) > span:nth-child(1)","parentid":"i_cecream","tag":"SPAN","xpath":"//*[@id=\"i_cecream\"]/div[1]/div[2]/div[1]/div[2]/div[1]/nav[1]/ul[1]/li[8]/span[1]/span[1]"},"index":0,"tagName":"SPAN"}}]})
        #鼠标点击
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221206143607416284,Title:鼠标点击,Note:')
        time.sleep(1)
        ichrome_firefox.do_click_pos_chrome(waitfor=10.000,run_mode='unctrl',button='left',curson='center',continue_on_error='break',title=r'艺赛旗RPA-哔哩哔哩_Bilibili',url=r'https://search.bilibili.com/*',attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"aaname":"艺赛旗RPA","css-selector":"#i_cecream > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > h2:nth-child(1) > a:nth-child(1)","parentid":"i_cecream","tag":"A","xpath":"//*[@id=\"i_cecream\"]/div[1]/div[2]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/h2[1]/a[1]"},"index":0,"tagName":"A"}}]})
        #进入“设计器组件功能学习视频”合集
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207135938836395,Title:进入“设计器组件功能学习视频”合集,BzTag:6cbcaeab0b8b44bb80d88694e622c7b1,Note:')
        irobot.send_robot_workflow(sz_tag='6cbcaeab0b8b44bb80d88694e622c7b1',sz_title='进入“设计器组件功能学习视频”合集')
        #鼠标移动
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2023020610331123042,Title:鼠标移动,Note:')
        ichrome_firefox.do_moveto_pos_chrome(waitfor=10.000,curson='leftbottom',title=r'艺赛旗RPA-哔哩哔哩_Bilibili',url=r'https://search.bilibili.com/*',attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"css-selector":"#i_cecream > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > nav:nth-child(1) > ul:nth-child(1)","parentid":"i_cecream","tag":"UL","xpath":"//*[@id=\"i_cecream\"]/div[1]/div[2]/div[1]/div[2]/div[1]/nav[1]/ul[1]"},"index":0,"tagName":"UL"}}]})
        #鼠标点击
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221206143607425285,Title:鼠标点击,Note:')
        time.sleep(1)
        ichrome_firefox.do_click_pos_chrome(waitfor=10.000,run_mode='unctrl',button='left',curson='center',continue_on_error='break',title=r'艺赛旗RPA的个人空间_哔哩哔哩_bilibili',url=r'https://space.bilibili.com/*',attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"aaname":"合集和列表","css-selector":"#navigator > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > a:nth-child(4) > span:nth-child(2)","parentid":"navigator","tag":"SPAN","xpath":"//*[@id=\"navigator\"]/div[1]/div[1]/div[1]/a[4]/span[2]"},"index":0,"tagName":"SPAN"}}]})
        #鼠标点击
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221206143607437286,Title:鼠标点击,Note:')
        time.sleep(1)
        ichrome_firefox.do_click_pos_chrome(waitfor=10.000,run_mode='unctrl',button='left',curson='center',continue_on_error='break',title=r'艺赛旗RPA的个人空间_哔哩哔哩_bilibili',url=r'https://space.bilibili.com/*',attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"css-selector":"#page-series-index > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > img:nth-child(1)","parentid":"page-series-index","tag":"IMG","xpath":"//*[@id=\"page-series-index\"]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/img[1]"},"index":0,"tagName":"IMG"}}]})
        time.sleep(3)
        #抓取所有视频的标题、播放量、发布时间
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207135958461399,Title:抓取所有视频的标题、播放量、发布时间,BzTag:6a7952e9a302409d9abae5e05b6214e2,Note:')
        irobot.send_robot_workflow(sz_tag='6a7952e9a302409d9abae5e05b6214e2',sz_title='抓取所有视频的标题、播放量、发布时间')
        #结构化抓取
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207134837668362,Title:结构化抓取,Note:')
        self.gv_1=ichrome_firefox.extract_data_chrome(title=r'艺赛旗RPA的个人空间_哔哩哔哩_bilibili',url=r'https://space.bilibili.com/*',metaData=[{"exact":1,"name":"_row_","path":[{"tag":"li"}]},{"attr":["text"],"exact":1,"name":"Column0","path":[{"tag":"li"},{"idx":2,"tag":"a"}]},{"attr":["text"],"exact":1,"name":"Column1","path":[{"tag":"li"},{"idx":1,"tag":"div"},{"idx":1,"tag":"span"}]},{"attr":["text"],"exact":1,"name":"Column2","path":[{"tag":"li"},{"idx":1,"tag":"div"},{"idx":2,"tag":"span"}]}],attrMap={"hasReachedRelativeAncestor":"false","isTable":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"css-selector":"#page-series-detail > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > ul:nth-child(2)","parentid":"page-series-detail","tag":"UL","xpath":"//*[@id=\"page-series-detail\"]/div[1]/div[1]/div[1]/ul[1]"},"index":0,"tagName":"UL"}}]},run_mode='unctrl',page_btn_attrMap={"hasReachedRelativeAncestor":"false","nodeHierarchyInfo":[{"isPresentInSelector":1,"otherAttributes":{},"selectorInfo":{"attributes":{"aaname":"下一页","css-selector":"#page-series-detail > div:nth-child(1) > div:nth-child(1) > ul:nth-child(2) > li:nth-child(7) > a:nth-child(1)","parentid":"page-series-detail","tag":"A","xpath":"//*[@id=\"page-series-detail\"]/div[1]/div[1]/ul[1]/li[7]/a[1]"},"index":0,"tagName":"A"}}]},image=r'snapshot_20221207135028879.png',columns_setting=[{"DataType":"text","IsTable":0,"IsVisible":1,"Name":"视频标题","ReferenceName":"Column0","Value":"【艺赛旗RPA】-流程管理","attr":"text"},{"DataType":"text","IsTable":0,"IsVisible":1,"Name":"播放量","NumType":"int","ReferenceName":"Column1","Value":"502","attr":"int"},{"DataType":"text","DateFormat":"%Y-%m-%d","IsTable":0,"IsVisible":1,"Name":"发布时间","ReferenceName":"Column2","Value":"2019-3-27","attr":"date"}],extract_mode=0,max_limit=500,img_res_path = self.path)
        print('[Main] [结构化抓取] [SNTag:20221207134837668362]  返回值：[' + str(type(self.gv_1)) + ']' + str(self.gv_1))
        #将抓取的数据写入excel表格中
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207140015315402,Title:将抓取的数据写入excel表格中,BzTag:46e9f5b34f2e43958687f37ffe3cea1d,Note:')
        irobot.send_robot_workflow(sz_tag='46e9f5b34f2e43958687f37ffe3cea1d',sz_title='将抓取的数据写入excel表格中')
        #获取环境路径
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811364047325,Title:获取环境路径,Note:')
        tvar_2022120811364047626=rpa_environment.get_sys_path(name='Desktop')
        print('[Main] [获取环境路径] [SNTag:2022120811364047325]  返回值：[' + str(type(tvar_2022120811364047626)) + ']' + str(tvar_2022120811364047626))
        #创建excel
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207133912551344,Title:创建excel,Note:')
        time.sleep(1)
        tvar_20221207133912551345=iexcel.create_excel(path=tvar_2022120811364047626,file_name='iS-RPA设计器组件功能学习视频')
        print('[Main] [创建excel] [SNTag:20221207133912551344]  返回值：[' + str(type(tvar_20221207133912551345)) + ']' + str(tvar_20221207133912551345))
        #导出excel
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:20221207134721579357,Title:导出excel,Note:')
        time.sleep(1)
        tvar_20221207134721579358=icsv.write_excel(path=tvar_20221207133912551345,df=self.gv_1)
        print('[Main] [导出excel] [SNTag:20221207134721579357]  返回值：[' + str(type(tvar_20221207134721579358)) + ']' + str(tvar_20221207134721579358))
        #文件
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022121510521971725,Title:文件,Note:')
        ics.run_shellexecute(path=tvar_20221207133912551345)
 
if __name__ == '__main__':
    ILog.begin_init()
    robot_no = ''
    proc_no = ''
    job_no = ''
    input_arg = ''
    try:
        argv = sys.argv[1:]
        opts, args = getopt.getopt(argv,"hr:p:j:i:",["robot = ","proc = ","job = ","input = "])
    except getopt.GetoptError:
        print ('robot.py -r <robot> -p <proc> -j <job>')
    for opt, arg in opts:
        if opt == '-h':
            print ('robot.py -r <robot> -p <proc> -j <job>')
        elif opt in ("-r", "--robot"):
            robot_no = arg
        elif opt in ("-p", "--proc"):
            proc_no = arg
        elif opt in ("-j", "--job"):
            job_no = arg
        elif opt in ("-i", "--input"):
            input_arg = arg
    pro = GetBilibiliData(robot_no=robot_no,proc_no=proc_no,job_no=job_no,input_arg=input_arg)
    pro.Main()
